package thread_pool.executors;

import org.junit.Test;
import thread_pool.TestThreadFactory;

import java.util.concurrent.*;

/**
 * @describe: 类描述信息
 * @author: morningcat.zhang
 * @date: 2019/4/22 2:25 PM
 */
public class ExecutorsTest {

    @Test
    public void test1() {
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        // 等同于

        /**
         * 核心线程数与最大线程数相同
         * 队列近似无限大
         */
        new ThreadPoolExecutor(10, 10,
                0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());


    }

    @Test
    public void test2() {
        ExecutorService executorService = Executors.newFixedThreadPool(10, new TestThreadFactory());
        // 等同于

        new ThreadPoolExecutor(10, 10,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>(),
                new TestThreadFactory());
    }

    /*****************************************************************************************************/

    @Test
    public void test3() {
        ExecutorService executorService = Executors.newCachedThreadPool();
        // 等同于

        /**
         * 核心线程数为零
         * 最大线程数近似无限大
         * 队列长度为零，无法保存任务
         */
        new ThreadPoolExecutor(0, Integer.MAX_VALUE,
                60L, TimeUnit.SECONDS,
                new SynchronousQueue<Runnable>());

    }

    @Test
    public void test4() {
        ExecutorService executorService = Executors.newSingleThreadExecutor();
        // 等同于

        /**
         * 核心线程数与最大线程数相同 均为1
         * 队列近似无限大
         */
        new ThreadPoolExecutor(1, 1,
                0L, TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>());

    }

    @Test
    public void test5() {
        ExecutorService executorService = Executors.newScheduledThreadPool(10);
        // 等同于

        new ScheduledThreadPoolExecutor(10);
    }

    @Test
    public void test6() {
        ExecutorService executorService = Executors.newSingleThreadScheduledExecutor();
        // 等同于

        new ScheduledThreadPoolExecutor(1);
    }

    /*****************************************************************************************************/

    @Test
    public void test7() {
        ExecutorService executorService = Executors.newWorkStealingPool();
        // 等同于

        new ForkJoinPool
                (Runtime.getRuntime().availableProcessors(),
                        ForkJoinPool.defaultForkJoinWorkerThreadFactory,
                        null, true);
    }

    @Test
    public void test8() {
        ExecutorService executorService = Executors.newWorkStealingPool(10);
        // 等同于

        new ForkJoinPool
                (10,
                        ForkJoinPool.defaultForkJoinWorkerThreadFactory,
                        null, true);
    }
}
